:- dynamic actualSituation/1,perception/2,executedAction/2,position/2,sabe/3.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                             Diagnostic rules                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

sabe(X,T,S) :- perception(X,T),actualSituation(S).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                             Successor state axioms                              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

est(S1) :- S1 > 0,S is S1-1,executedAction(requestForecastRequest,S),position(initial,S),sabe(waitingForRequest,_,S),asserta(position(requestForecastRequest,S1)),tiempo(X,_),random(1,3,R),X1 is X+R,asserta(tiempo(X1,S1)).
est(S1) :- S1 > 0,S is S1-1,executedAction(informPOSForecast,S),position(requestForecastRequest,S),sabe(agreeForecastRequestResponse,_,S),asserta(position(informPOSForecastSent,S1)),tiempo(X,_),random(1,2,R),random(12,96,K),X1 is X+R+K,asserta(tiempo(X1,S1)).
est(S1) :- S1 > 0,S is S1-1,executedAction(informPlannedEvents,S),position(requestForecastRequest,S),sabe(agreeForecastRequestResponse,_,S),asserta(position(informPlannedEventsSent,S1)),tiempo(X,_),random(2,4,R),random(30,150,K),X1 is X+R+K,asserta(tiempo(X1,S1)).
est(S1) :- S1 > 0,S is S1-1,executedAction(acceptProposalDemandForecastResponse,S),position(informPOSForecastSent,S),sabe(proposeDemandForecast1,_,S),asserta(position(acceptProposalSent,S1)),tiempo(X,_),random(1,3,R),X1 is X+R,asserta(tiempo(X1,S1)).
est(S1) :- S1 > 0,S is S1-1,executedAction(acceptProposalDemandForecastResponse,S),position(informPlannedEventsSent,S),sabe(proposeDemandForecast1,_,S),asserta(position(acceptProposalSent,S1)),tiempo(X,_),random(1,3,R),X1 is X+R,asserta(tiempo(X1,S1)).
%est(S1) :- S1 > 0,S is S1-1,executedAction(proposeUpdatedDemandForecast,S),position(informPlannedEventsSent,S),sabe(proposeDemandForecast1,_,S),asserta(position(UpdatedDemandForecastSent,S1)),tiempo(X,_),random(1,2,R),X1 is X+R,asserta(tiempo(X1,S1)).
%est(S1) :- S1 > 0,S is S1-1,executedAction(proposeUpdatedDemandForecast,S),position(informPOSForecastSent,S),sabe(proposeDemandForecast1,_,S),asserta(position(UpdatedDemandForecastSent,S1)),tiempo(X,_),random(1,2,R),X1 is X+R,asserta(tiempo(X1,S1)).
est(S1) :- S1 > 0,S is S1-1,executedAction(endSubprocess3,S),position(acceptProposalSent,S),sabe(proposeDemandForecast2,_,S),asserta(position(subprocess3Finalized,S1)),tiempo(X,_),random(1,3,R),X1 is X+R,asserta(tiempo(X1,S1)).
est(S1) :- S1 > 0,S is S1-1,executedAction(endSubprocess3,S),position(updatedProposalSent,S),sabe(proposeDemandForecast2,_,S),asserta(position(subprocess3Finalized,S1)),tiempo(X,_),random(1,3,R),X1 is X+R,asserta(tiempo(X1,S1)).
est(S1) :- S1 > 0,S is S1-1,executedAction(endEventGoal,S),position(subprocess3Finalized,S),sabe(informIssues,_,S),asserta(position(success,S1)),tiempo(X,_),random(1,3,R),X1 is X+R,asserta(tiempo(X1,S1)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                             Actions Evaluations                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

excelent(requestForecastRequest,S) :- position(initial,S),sabe(waitingForRequest,_,S).
excelent(cancelDemandForecast,S) :- sabe(refuseForecastRequestResponse,_,S).
excelent(informPOSForecast,S) :- not(position(requestForecastRequest,S)),sabe(agreeForecastRequestResponse,_,S).
excelent(informPlannedEvents,S) :- position(requestForecastRequest,S),sabe(agreeForecastRequestResponse,_,S).
excelent(acceptProposalDemandForecastResponse,S) :- position(informPOSForecastSent,S),sabe(proposeDemandForecast1,_,S).
excelent(acceptProposalDemandForecastResponse,S) :- position(informPlannedEventsSent,S),sabe(proposeDemandForecast1,_,S).
excelent(proposeUpdatedDemandForecast,S) :- position(informPOSForecastSent,S),sabe(proposeDemandForecast1,_,S).
excelent(proposeUpdatedDemandForecast,S) :- position(informPlannedEventsSent,S),sabe(proposeDemandForecast1,_,S).
excelent(endSubprocess3,S) :- position(acceptProposalSent,S),sabe(proposeDemandForecast2,_,S).
excelent(endEventGoal,S) :- position(subprocess3Finalized,S),sabe(informIssues,_,S).

bestAction(noAction,S) :- goalReached(S),!.
bestAction(X,S) :- excelent(X,S),!.
bestAction(_,S) :- goalReached(S),!.

goalReached(S) :- position(success,S).
